﻿using smp.QueryDb.Structure;

namespace smp.QueryDb
{
    public class Filter
    {
        private readonly FilterStructure _structure;

        public Filter(Column column)
        {
            _structure = new FilterStructure(column);
        }

        internal FilterStructure Structure { get { return _structure; } }

        public IFilterOperator Is
        {
            get { return new FilterOperator(this, false); }
        }

        public IFilterOperator IsNot
        {
            get
            {
                return new FilterOperator(this, true);
            }
        }

        public Filter And(Column column)
        {
            var newFilter = new Filter(column);
            newFilter.And(this);
            return newFilter;
        }

        public Filter And(Filter filter)
        {
            if (!_structure.HasBaseOperation)
            {
                throw Helper.Error("Invalid Filter And statement, you must define the base operation before applying logical gates.");
            }
            filter.Structure.SetLogic("AND");
            _structure.AddAdditionalFilter(filter.Structure);
            return this;
        }

        public Filter Or(Column column)
        {
            var newFilter = new Filter(column);
            newFilter.Or(this);
            return newFilter;
        }

        public Filter Or(Filter filter)
        {
            if (!_structure.HasBaseOperation)
            {
                throw Helper.Error("Invalid Filter And statement, you must define the base operation before applying logical gates.");
            }
            filter.Structure.SetLogic("OR");
            _structure.AddAdditionalFilter(filter.Structure);
            return this;
        }
    }
}
